package com.huayue.point.controller;

import com.huayue.user.bean.UserDto;
import org.apache.dubbo.common.utils.StringUtils;

import javax.servlet.http.HttpServletRequest;

public interface BaseController {
    /**
     * 判断是否管理员，若不是，当errorMsg不为空时抛出异常，否则返回false，若是管理员则返回true
     * @param request
     * @param errorMsg
     * @return
     * @throws Exception
     */
    default boolean checkAdmin(HttpServletRequest request, String errorMsg) throws Exception {
        UserDto user = (UserDto)request.getSession().getAttribute("user");
        Boolean admin = user.isAdmin();
        if (!admin) {
            if(StringUtils.isNotEmpty(errorMsg)) {
                throw new Exception(errorMsg);
            }
            return  false;
        }
        return true;
    }
}
